<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div class="article-body">
    <div class="article-intro">

        <h1>Java 变量类型</h1>
        <p>在Java语言中，所有的变量在使用前必须声明。声明变量的基本格式如下：</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-identifier">type</span><span class="hl-code"> </span><span class="hl-identifier">identifier</span><span class="hl-code"> </span><span class="hl-brackets">[</span><span class="hl-code"> = </span><span class="hl-identifier">value</span><span class="hl-brackets">]</span><span class="hl-brackets">[</span><span class="hl-code">, </span><span class="hl-identifier">identifier</span><span class="hl-code"> </span><span class="hl-brackets">[</span><span class="hl-code">= </span><span class="hl-identifier">value</span><span class="hl-brackets">]</span><span class="hl-code"> ...</span><span class="hl-brackets">]</span><span class="hl-code"> ;</span></pre>
            </div>
        </div>
        <p>格式说明：type为Java数据类型。identifier是变量名。可以使用逗号隔开来声明多个同类型变量。</p>
        <p>以下列出了一些变量的声明实例。注意有些包含了初始化过程。</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">, </span><span class="hl-identifier">b</span><span class="hl-code">, </span><span class="hl-identifier">c</span><span class="hl-code">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-comment">//</span><span class="hl-comment"> 声明三个int型整数：a、 b、c</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">d</span><span class="hl-code"> = </span><span class="hl-number">3</span><span class="hl-code">, </span><span class="hl-identifier">e</span><span class="hl-code"> = </span><span class="hl-number">4</span><span class="hl-code">, </span><span class="hl-identifier">f</span><span class="hl-code"> = </span><span class="hl-number">5</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 声明三个整数并赋予初值</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">byte</span><span class="hl-code"> </span><span class="hl-identifier">z</span><span class="hl-code"> = </span><span class="hl-number">22</span><span class="hl-code">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-comment">//</span><span class="hl-comment"> 声明并初始化 z</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">s</span><span class="hl-code"> = </span><span class="hl-quotes">"</span><span class="hl-string">runoob</span><span class="hl-quotes">"</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment"> 声明并初始化字符串 s</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">pi</span><span class="hl-code"> = </span><span class="hl-number">3</span><span class="hl-number">.14159</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 声明了双精度浮点型变量 pi</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">char</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">x</span><span class="hl-quotes">'</span><span class="hl-code">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="hl-comment">//</span><span class="hl-comment"> 声明变量 x 的值是字符 'x'。</span><span class="hl-comment"></span></pre>
            </div>
        </div>
        <p>Java语言支持的变量类型有：</p>
        <ul>
            <li>
                类变量：独立于方法之外的变量，用 static 修饰。</li>
            <li>
                实例变量：独立于方法之外的变量，不过没有 static 修饰。  </li>
            <li>
                局部变量：类的方法中的变量。</li>
        </ul>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Variable</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">allClicks</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">;    </span><span class="hl-comment">//</span><span class="hl-comment"> 类变量</span><span class="hl-comment"></span><span class="hl-code">

    </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">str</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">hello world</span><span class="hl-quotes">"</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment"> 实例变量</span><span class="hl-comment"></span><span class="hl-code">

    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">method</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">

        </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> =</span><span class="hl-number">0</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment"> 局部变量</span><span class="hl-comment"></span><span class="hl-code">

    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <hr>
        <h2>Java 局部变量</h2>
        <ul>
            <li>
                局部变量声明在方法、构造方法或者语句块中；</li>
            <li>
                局部变量在方法、构造方法、或者语句块被执行的时候创建，当它们执行完成后，变量将会被销毁；</li>
            <li>
                访问修饰符不能用于局部变量；</li>
            <li>
                局部变量只在声明它的方法、构造方法或者语句块中可见；</li>
            <li>
                局部变量是在栈上分配的。</li>
            <li>
                局部变量没有默认值，所以局部变量被声明后，必须经过初始化，才可以使用。</li>
        </ul>
        <h3>实例 1</h3>
        <p>在以下实例中age是一个局部变量。定义在pupAge()方法中，它的作用域就限制在这个方法中。</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">package</span><span class="hl-code"> </span><span class="hl-identifier">com</span><span class="hl-code">.</span><span class="hl-identifier">runoob</span><span class="hl-code">.</span><span class="hl-identifier">test</span><span class="hl-code">;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">pupAge</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;
      </span><span class="hl-identifier">age</span><span class="hl-code"> = </span><span class="hl-identifier">age</span><span class="hl-code"> + </span><span class="hl-number">7</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">小狗的年龄是: </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">age</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">Test</span><span class="hl-code"> </span><span class="hl-identifier">test</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">test</span><span class="hl-code">.</span><span class="hl-identifier">pupAge</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div><p>以上实例编译运行结果如下:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">小狗的年龄是:</span><span class="pln"> </span><span class="lit">7</span></pre>
        <h3>实例 2</h3>
        <p>在下面的例子中 age 变量没有初始化，所以在编译时会出错：</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">package</span><span class="hl-code"> </span><span class="hl-identifier">com</span><span class="hl-code">.</span><span class="hl-identifier">runoob</span><span class="hl-code">.</span><span class="hl-identifier">test</span><span class="hl-code">;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">pupAge</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span class="hl-code">;
      </span><span class="hl-identifier">age</span><span class="hl-code"> = </span><span class="hl-identifier">age</span><span class="hl-code"> + </span><span class="hl-number">7</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">小狗的年龄是 : </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">age</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">Test</span><span class="hl-code"> </span><span class="hl-identifier">test</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">test</span><span class="hl-code">.</span><span class="hl-identifier">pupAge</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>以上实例编译运行结果如下:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Test</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit">4</span><span class="pun">:</span><span class="pln">variable number might </span><span class="kwd">not</span><span class="pln"> have been initialized
age </span><span class="pun">=</span><span class="pln"> age </span><span class="pun">+</span><span class="pln"> </span><span class="lit">7</span><span class="pun">;</span><span class="pln">
         </span><span class="pun">^</span><span class="pln">
</span><span class="lit">1</span><span class="pln"> error</span></pre>
        <hr>
        <h2>实例变量</h2>
        <ul>
            <li>
                实例变量声明在一个类中，但在方法、构造方法和语句块之外；</li>
            <li>
                当一个对象被实例化之后，每个实例变量的值就跟着确定；</li>
            <li>
                实例变量在对象创建的时候创建，在对象被销毁的时候销毁；</li>
            <li>
                实例变量的值应该至少被一个方法、构造方法或者语句块引用，使得外部能够通过这些方式获取实例变量信息；</li>
            <li>
                实例变量可以声明在使用前或者使用后；</li>
            <li>
                访问修饰符可以修饰实例变量；</li>
            <li>
                实例变量对于类中的方法、构造方法或者语句块是可见的。一般情况下应该把实例变量设为私有。通过使用访问修饰符可以使实例变量对子类可见；</li>
            <li>
                实例变量具有默认值。数值型变量的默认值是0，布尔型变量的默认值是false，引用类型变量的默认值是null。变量的值可以在声明时指定，也可以在构造方法中指定；</li>
            <li>
                实例变量可以直接通过变量名访问。但在静态方法以及其他类中，就应该使用完全限定名：ObejectReference.VariableName。</li>
        </ul>
        <h3>实例</h3>
        <div class="example">
            <h2 class="example">Employee.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">io</span><span class="hl-code">.*;
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Employee</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-comment">//</span><span class="hl-comment"> 这个实例变量对子类可见</span><span class="hl-comment"></span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">;
   </span><span class="hl-comment">//</span><span class="hl-comment"> 私有变量，仅在该类可见</span><span class="hl-comment"></span><span class="hl-code">
   </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">salary</span><span class="hl-code">;
   </span><span class="hl-comment">//</span><span class="hl-comment">在构造器中对name赋值</span><span class="hl-comment"></span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Employee</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">empName</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">name</span><span class="hl-code"> = </span><span class="hl-identifier">empName</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-comment">//</span><span class="hl-comment">设定salary的值</span><span class="hl-comment"></span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setSalary</span><span class="hl-brackets">(</span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">empSal</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">salary</span><span class="hl-code"> = </span><span class="hl-identifier">empSal</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-comment">//</span><span class="hl-comment"> 打印信息</span><span class="hl-comment"></span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">printEmp</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">名字 : </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">name</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">薪水 : </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">salary</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">Employee</span><span class="hl-code"> </span><span class="hl-identifier">empOne</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Employee</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">RUNOOB</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">empOne</span><span class="hl-code">.</span><span class="hl-identifier">setSalary</span><span class="hl-brackets">(</span><span class="hl-number">1000</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">empOne</span><span class="hl-code">.</span><span class="hl-identifier">printEmp</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>以上实例编译运行结果如下:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ javac </span><span class="typ">Employee</span><span class="pun">.</span><span class="pln">java
$ java </span><span class="typ">Employee</span><span class="pln">
</span><span class="pun">名字</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> RUNOOB
</span><span class="pun">薪水</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="lit">1000.0</span></pre>
        <hr>
        <h2>类变量（静态变量）</h2>
        <ul>
            <li>
                类变量也称为静态变量，在类中以static关键字声明，但必须在方法构造方法和语句块之外。</li>
            <li>
                无论一个类创建了多少个对象，类只拥有类变量的一份拷贝。</li>
            <li>
                静态变量除了被声明为常量外很少使用。常量是指声明为public/private，final和static类型的变量。常量初始化后不可改变。</li>
            <li>
                静态变量储存在静态存储区。经常被声明为常量，很少单独使用static声明变量。</li>
            <li>
                静态变量在程序开始时创建，在程序结束时销毁。</li>
            <li>
                与实例变量具有相似的可见性。但为了对类的使用者可见，大多数静态变量声明为public类型。</li>
            <li>
                默认值和实例变量相似。数值型变量默认值是0，布尔型默认值是false，引用类型默认值是null。变量的值可以在声明的时候指定，也可以在构造方法中指定。此外，静态变量还可以在静态语句块中初始化。</li>
            <li>
                静态变量可以通过：<em>ClassName.VariableName</em>的方式访问。</li>
            <li>
                类变量被声明为public static final类型时，类变量名称一般建议使用大写字母。如果静态变量不是public和final类型，其命名方式与实例变量以及局部变量的命名方式一致。</li>
        </ul>
        <p>实例：</p>
        <div class="example">
            <h2 class="example">Employee.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">io</span><span class="hl-code">.*;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Employee</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-comment">//</span><span class="hl-comment">salary是静态的私有变量</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">salary</span><span class="hl-code">;
    </span><span class="hl-comment">//</span><span class="hl-comment"> DEPARTMENT是一个常量</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">final</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">DEPARTMENT</span><span class="hl-code"> = </span><span class="hl-quotes">"</span><span class="hl-string">开发人员</span><span class="hl-quotes">"</span><span class="hl-code">;
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">salary</span><span class="hl-code"> = </span><span class="hl-number">10000</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">DEPARTMENT</span><span class="hl-code">+</span><span class="hl-quotes">"</span><span class="hl-string">平均工资:</span><span class="hl-quotes">"</span><span class="hl-code">+</span><span class="hl-identifier">salary</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>以上实例编译运行结果如下:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">开发人员平均工资:</span><span class="lit">10000.0</span></pre>
        <p><strong>注意：</strong>如果其他类想要访问该变量，可以这样访问：<strong>Employee.DEPARTMENT</strong>。</p>
        <p>本章节中我们学习了Java的变量类型，下一章节中我们将介绍Java修饰符的使用。</p>

    </div>
</div>
</body>
</html>
